<?php
/**
 * 配置
 */
class Conf{
	private static $conf;

	public static function get($k) {
		return self::$conf[$k];
	}
	public static function set($k,$v) {
		self::$conf[$k] = $v;
		return true;
	}
	public static function init() {
		self::$conf = file_exists(EtcRoot."/conf.inc.php")?require EtcRoot."/conf.inc.php":false;
	}
}

/**
 * 路由
 */
class Router{
    private static $routerconf;

    public static function init() {
    	self::$routerconf =   (EtcRoot."/router.inc.php")?require EtcRoot."/router.inc.php":false;
    }
    public static function analyze() {
        $url = strtolower($_SERVER["REQUEST_URI"]);
        $method = strtolower($_SERVER["REQUEST_METHOD"]);
        foreach (self::$routerconf as $k => $v) {
        	$pats = explode(" ", $v['pattern']);
        	if(strtolower($pats[0]) === $method){
        		if(self::check($url, $pats[1])){
                    return $v;
                }
            }
        }
        

    }
    public static function check($uri, $regex) {
		$regex = ltrim(rtrim($regex, "/"));
		$regex = "/".str_replace("/", "\/", $regex).'(?:\?.*?)*\/?$/';
        if(preg_match($regex, $uri, $t)){
            return true;
        }
        return false;
    }
    public static function set() {
        
    }

    public static function get() {
        
    }
}
/**
 * App
 */
class App{
    public static $HandlerClass;
    public static $HandlerFunction;
    public static $HandlerPath;
	public static function run() {
		$t = Router::analyze();
        list(self::$HandlerClass, self::$HandlerFunction) = explode("#",$t['handler']);
        self::$HandlerPath = AppRoot.'/'.self::$HandlerClass.'.Handler.php';
        self::$HandlerClass = self::$HandlerClass."Handler";
        //通过反射实例化类和方法
        if(file_exists(self::$HandlerPath)){
            require_once self::$HandlerPath;
            $rclass = new ReflectionClass(self::$HandlerClass);
            if($rclass->hasMethod(self::$HandlerFunction)){
                $instance  = $rclass->newInstanceArgs();
                $rmethod = $rclass->getmethod(self::$HandlerFunction);
                $rmethod->invokeArgs($instance, array(''));
            }
        }
	}
}